﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void addOrder()
        {

            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            Assert.AreEqual(orderList[0].Cname, service.orderList[0].Cname);
            Assert.AreEqual(orderList[0].Amount, service.orderList[0].Amount);
            Assert.AreEqual(orderList[0].Customername, service.orderList[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, service.orderList[0].Numbers);
        }
        [TestMethod]
        public void deleteOrder()
        {
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.Order order1 = new OrderExer.Order(2, "test2", "test2", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order);
            service.addOrder(order1);
            service.deleteOrder(2);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            Assert.AreEqual(orderList[0].Cname, service.orderList[0].Cname);
            Assert.AreEqual(orderList[0].Amount, service.orderList[0].Amount);
            Assert.AreEqual(orderList[0].Customername, service.orderList[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, service.orderList[0].Numbers);
        }

        [TestMethod]
        public void modifyOrder()
        {
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.Order order1 = new OrderExer.Order(1, "test", "miao", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order1);
            service.modifyOrder(1, "miao");
            Assert.AreEqual(orderList[0].Cname, service.orderList[0].Cname);
            Assert.AreEqual(orderList[0].Amount, service.orderList[0].Amount);
            Assert.AreEqual(orderList[0].Customername, service.orderList[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, service.orderList[0].Numbers);
        }

        [TestMethod]
        public void sort1()
        {
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.Order order1 = new OrderExer.Order(2, "test", "miao", 1, 1);
            OrderExer.Order order2 = new OrderExer.Order(3, "test", "miao", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order2);
            service.addOrder(order1);
            service.addOrder(order);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            orderList.Add(order1);
            orderList.Add(order2);
            service.sort();
            Assert.AreEqual(orderList[0].Cname, service.orderList[0].Cname);
            Assert.AreEqual(orderList[0].Amount, service.orderList[0].Amount);
            Assert.AreEqual(orderList[0].Customername, service.orderList[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, service.orderList[0].Numbers);
            Assert.AreEqual(orderList[1].Cname, service.orderList[1].Cname);
            Assert.AreEqual(orderList[1].Amount, service.orderList[1].Amount);
            Assert.AreEqual(orderList[1].Customername, service.orderList[1].Customername);
            Assert.AreEqual(orderList[1].Numbers, service.orderList[1].Numbers);
            Assert.AreEqual(orderList[2].Cname, service.orderList[2].Cname);
            Assert.AreEqual(orderList[2].Amount, service.orderList[2].Amount);
            Assert.AreEqual(orderList[2].Customername, service.orderList[2].Customername);
            Assert.AreEqual(orderList[2].Numbers, service.orderList[2].Numbers);
        }

        [TestMethod]
        public void sort2()
        {
            OrderExer.Order order = new OrderExer.Order(1, "test", "test", 1, 1);
            OrderExer.Order order1 = new OrderExer.Order(2, "test", "miao", 80, 1);
            OrderExer.Order order2 = new OrderExer.Order(3, "test", "miao", 100, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            service.addOrder(order2);
            service.addOrder(order1);
            service.addOrder(order);
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            orderList.Add(order1);
            orderList.Add(order2);
            service.sort((x, y) => x.Amount.CompareTo(y.Amount));
            Assert.AreEqual(orderList[0].Cname, service.orderList[0].Cname);
            Assert.AreEqual(orderList[0].Amount, service.orderList[0].Amount);
            Assert.AreEqual(orderList[0].Customername, service.orderList[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, service.orderList[0].Numbers);
            Assert.AreEqual(orderList[1].Cname, service.orderList[1].Cname);
            Assert.AreEqual(orderList[1].Amount, service.orderList[1].Amount);
            Assert.AreEqual(orderList[1].Customername, service.orderList[1].Customername);
            Assert.AreEqual(orderList[1].Numbers, service.orderList[1].Numbers);
            Assert.AreEqual(orderList[2].Cname, service.orderList[2].Cname);
            Assert.AreEqual(orderList[2].Amount, service.orderList[2].Amount);
            Assert.AreEqual(orderList[2].Customername, service.orderList[2].Customername);
            Assert.AreEqual(orderList[2].Numbers, service.orderList[2].Numbers);
        }

        [TestMethod]
        public void ExportAndImport()
        {
            OrderExer.Order order = new OrderExer.Order(1, "1", "1", 1, 1);
            OrderExer.OrderService service = new OrderExer.OrderService();
            XmlSerializer xmlser = new XmlSerializer(typeof(OrderExer.Order[]));
            String xmlFileName = "order.xml";//定义xml文件名字
            service.addOrder(order);
            OrderExer.Order[] temp = new OrderExer.Order[service.orderList.Count];
            service.orderList.CopyTo(temp);

            //XML序列化

            FileStream fs = new FileStream(xmlFileName, FileMode.Create);
            xmlser.Serialize(fs, temp);
            fs.Close();

            //显示xml文本
            string xml = File.ReadAllText(xmlFileName);
            OrderExer.Order[] temp1;
            using (FileStream fs1 = new FileStream(xmlFileName, FileMode.Open))
            {
                temp1 = (OrderExer.Order[])xmlser.Deserialize(fs1);
                Array.ForEach(temp1, p => Console.WriteLine(p));
            }
            List<OrderExer.Order> orderList = new List<OrderExer.Order>();
            orderList.Add(order);
            Assert.AreEqual(orderList[0].Cname, temp1[0].Cname);
            Assert.AreEqual(orderList[0].Amount, temp1[0].Amount);
            Assert.AreEqual(orderList[0].Customername, temp1[0].Customername);
            Assert.AreEqual(orderList[0].Numbers, temp1[0].Numbers);

        }
    }
}
